<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/5/11
 * Time: 10:17
 */
namespace Admin\Widget;

use Admin\Controller\AdminController;
use Tool\Api\CityApi;
use Tool\Api\ProvinceApi;
use Admin\Api\VUserGroupInfoApi;
use Admin\Api\OrgMemberApi;

/**
 * 地区管理员所属地区
 */
class OrgAreaWidget extends AdminController{


    public function Area(){
        $permisson= $this->check_manager_permisson();
        if($permisson !=7){
            //不是地区管理员
            return;
        }
        $this->assign('permisson',$permisson);
        $province = $this->getOrgArea();
        $this->assign("area",$province);
        echo $this->fetch("Widget:org_area");
    }

    /**
     * 判断管理人员权限
     */
    public function check_manager_permisson(){

        if(UID == 1){
            //管理员,所有权限
            return 1;
        }

        $result = apiCall(VUserGroupInfoApi::GET_INFO, array(array('id'=>UID)));

        if(!$result['status']){
            $this->error($result['info']);
        }

        $info = $result['info'];

        if($info['group_id'] == 7){
            //地区管理员
        }

        return $info['group_id'];

    }

    /**
     * 查找区域管理员所属区域
     */
    public function getOrgArea(){

        $cityid = '';
        $map['type'] = 1;
        $map['member_uid'] = UID;
        $result = apiCall(OrgMemberApi::QUERY_NO_PAGING,array($map));

        if($result['status']){
            foreach($result['info'] as $val){
                $cityid = $cityid.$val['organization_id'].',';
            }
        }

        $provinceid = '';
        $map['type'] = 0;
        $result = apiCall(OrgMemberApi::QUERY_NO_PAGING,array($map));

        if($result['status']){
            foreach($result['info'] as $val){
                $provinceid = $provinceid.$val['organization_id'].',';
            }
        }

        $result = apiCall(ProvinceApi::QUERY_NO_PAGING,array());
        if(!$result['status']) $this->error('未知错误');

        $province = $result['info'];
        $result = apiCall(CityApi::QUERY_NO_PAGING,array());
        if(!$result['status']) $this->error('未知错误');

        $city = $result['info'];
        $result = apiCall(OrgMemberApi::QUERY_NO_PAGING,array(array('member_uid'=>UID)));
        if($result['status']){
            $org_member = $result['info'];
        }else{
            $org_member = array();
        }
        foreach($province as &$pro){
            $pro['checked'] = 0;
            foreach($org_member as $member){
                if($member['organization_id'] == $pro['provinceid']){
                    $pro['checked'] = 1;
                    break;
                }
            }
            foreach($city as &$vo){
                $vo['checked'] = 0;
                if($vo['father'] == $pro['provinceid']){

                    if(!isset($pro['_child'])){
                        $pro['_child'] = array();
                    }

                    if($vo['city'] == "市辖区" || $vo['city'] == "县"){
                        continue;
                    }

                    foreach($org_member as $member){
                        if($member['organization_id'] == $vo['cityid']){
                            $vo['checked'] = 1;
                            $pro['checked'] = 1;
                            break;
                        }
                    }
                    array_push($pro['_child'],$vo);
                }
            }
        }

        return $province;

    }

}